package com.example.magictowerx;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.MotionEvent;

public class MainMenu extends GameView
{
    private String[]	menu		= { "新游戏", "读取上次游戏", "帮助", "关于", "退出" };
    private int			itemNum		= menu.length;
    private int			curItem;
    private boolean		isFirstPlay	= false;
    public int			borderX, borderY;
    private Bitmap		mImgMenuBG	= null;

    private MainGame	mMainGame	= null;

    private int			split		= MySet.TextSize + 5;

    private int			y;
    private Paint		paint		= null;


    public MainMenu(Context context, MainGame mainGame)
    {
        super(context);
        borderX = 0;
        borderY = 0;

        paint = new Paint();
        paint.setTextSize(MySet.TextSize);
        paint.setFlags(Paint.ANTI_ALIAS_FLAG);
        mMainGame = mainGame;

        mImgMenuBG = BitmapFactory.decodeResource(this.getResources(), R.mipmap.menu_1);
    }


    private void next()
    {
        if ((curItem + 1) < itemNum)
            curItem++;
        else
            curItem = 0;
        if ((curItem == 1) && (isFirstPlay))
        {
            curItem++;
        }
    }


    private void pre()
    {
        if ((curItem - 1) >= 0)
            curItem--;
        else
            curItem = itemNum - 1;
        if ((curItem == 1) && (isFirstPlay))
        {
            curItem--;
        }
    }


    protected void onDraw(Canvas canvas)
    {
        paint.setColor(Color.BLACK);
        MySet.fillRect(canvas, 0, 0, MySet.SCREENW, MySet.SCREENH, paint);
        drawCont(canvas);
        MySet.drawImage(canvas, mImgMenuBG, (MySet.SCREENW - mImgMenuBG.getWidth()) / 2, 60);
        drawItem(canvas);
        y = (curItem + 1) * split + borderY + 600;

        paint.setARGB(255, 255, 0, 0);

        MySet.drawString(canvas, menu[curItem], borderX + (MySet.BORDERW - paint.measureText(menu[curItem])) / 2, y, paint);
    }

    private void drawCont(Canvas canvas) //操作栏
    {
        MySet.drawImage(canvas, getImage(GameScreen.IMAGE_WHEEL), 160, 1350, 400, 400, 0, 0);
        MySet.drawImage(canvas, getImage(GameScreen.IMAGE_BTNA), 600, 1380, 240, 240, 0, 0);
        MySet.drawImage(canvas, getImage(GameScreen.IMAGE_BTNB), 800, 1520, 240, 240, 0, 0);
    }

    public Bitmap getImage(int type)
    {
        Bitmap result = null;
        switch (type)
        {
            case  GameScreen.IMAGE_WHEEL:
                result = BitmapFactory.decodeResource(this.getResources(), R.mipmap.wheel_1);
                break;
            case  GameScreen.IMAGE_BTNA:
                result = BitmapFactory.decodeResource(this.getResources(), R.mipmap.btna_1);
                break;
            case  GameScreen.IMAGE_BTNB:
                result = BitmapFactory.decodeResource(this.getResources(), R.mipmap.btnb_1);
                break;
        }
        return result;
    }

    public boolean onTouchEvent(MotionEvent event)
    {
        int x = (int) event.getX();
        int y = (int) event.getY();
        int type = 0;
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                // 记录触摸点坐标
                if (MySet.touchUp(x,y) )
                {
                    pre();
                }
                else if (MySet.touchDown(x,y))
                {
                    next();
                }
                else if (MySet.touchOK(x,y))
                {
                    dealItem();
                }
                break;
            case MotionEvent.ACTION_UP:
                break;
        }
        return true;
    }

    public boolean onKeyUp(int keyCode)
    {
        switch (keyCode)
        {
            case MySet.KEY_DPAD_UP:
                pre();
                break;
            case MySet.KEY_DPAD_DOWN:
                next();
                break;
            case MySet.KEY_DPAD_OK:
                dealItem();
                break;
            case MySet.KEY_SOFT_RIGHT:
                mMainGame.getMagicTower().finish();
                break;
        }
        return true;
    }


    public boolean onKeyDown(int keyCode)
    {
        return true;
    }


    private void drawItem(Canvas canvas)
    {
        paint.setColor(Color.WHITE);
        for (int i = 0; i < itemNum; i++)
        {
            y = (i + 1) * split + borderY + 600;

            MySet.drawString(canvas, menu[i], borderX + (MySet.BORDERW - paint.measureText(menu[i])) / 2, y, paint);
        }
    }


    private void dealItem()
    {
        switch (curItem)
        {
            case 0:
                mMainGame.controlView(MySet.GAME_RUN);
                if (mMainGame.mbMusic == 1)
                {
                    mMainGame.mCMIDIPlayer.PlayMusic(2);
                }
                break;
            case 1:
                mMainGame.controlView(MySet.GAME_CONTINUE);
                if (mMainGame.mbMusic == 1)
                {
                    mMainGame.mCMIDIPlayer.PlayMusic(2);
                }
                break;
            case 2:
                mMainGame.controlView(MySet.GAME_HELP);
                break;
            case 3:
                mMainGame.controlView(MySet.GAME_ABOUT);
                break;
            case 4:
                if (mMainGame.mCMIDIPlayer != null)
                {
                    mMainGame.mCMIDIPlayer.FreeMusic();
                    mMainGame.mCMIDIPlayer = null;
                }
                System.gc();
                mMainGame.getMagicTower().finish();
                break;
        }
    }


    public void refurbish()
    {

    }


    public void reCycle()
    {
        mImgMenuBG = null;
        paint = null;
        System.gc();
    }
}

